package cn.iocoder.yudao.module.wms.service.stockserial;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.wms.controller.admin.stockserial.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.stockserial.StockSerialDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wms.dal.mysql.stockserial.StockSerialMapper;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.*;

/**
 * 库存序列号 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class StockSerialServiceImpl implements StockSerialService {

    @Resource
    private StockSerialMapper stockSerialMapper;

    @Override
    public Long createStockSerial(StockSerialSaveReqVO createReqVO) {
        // 插入
        StockSerialDO stockSerial = BeanUtils.toBean(createReqVO, StockSerialDO.class);
        stockSerialMapper.insert(stockSerial);
        // 返回
        return stockSerial.getId();
    }

    @Override
    public void updateStockSerial(StockSerialSaveReqVO updateReqVO) {
        // 校验存在
        validateStockSerialExists(updateReqVO.getId());
        // 更新
        StockSerialDO updateObj = BeanUtils.toBean(updateReqVO, StockSerialDO.class);
        stockSerialMapper.updateById(updateObj);
    }

    @Override
    public void deleteStockSerial(Long id) {
        // 校验存在
        validateStockSerialExists(id);
        // 删除
        stockSerialMapper.deleteById(id);
    }

    private void validateStockSerialExists(Long id) {
        if (stockSerialMapper.selectById(id) == null) {
            throw exception(STOCK_SERIAL_NOT_EXISTS);
        }
    }

    @Override
    public StockSerialDO getStockSerial(Long id) {
        return stockSerialMapper.selectById(id);
    }

    @Override
    public PageResult<StockSerialDO> getStockSerialPage(StockSerialPageReqVO pageReqVO) {
        return stockSerialMapper.selectPage(pageReqVO);
    }

}